package org.yu.api.register;

import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;

import java.util.Arrays;

public  class BlockEntityTypeBuilder<T extends BlockEntity> {
    private final BlockEntityTypeHolder.BESupplier<T> blockEntitySupplier;
    private final LazyRegistry lazyRegistry;
    private final String name;
    private BlockHolder<?>[] blockHolders;
    private BlockEntityTypeHolder<T> result = new BlockEntityTypeHolder<>();

    protected BlockEntityTypeBuilder(LazyRegistry lazyRegistry, String name, BlockEntityTypeHolder.BESupplier<T> blockEntitySupplier) {
        this.blockEntitySupplier = blockEntitySupplier;
        this.lazyRegistry = lazyRegistry;
        this.name = name;
    }

    public BlockEntityTypeBuilder<T> withBlocks(BlockHolder<?>... blockHolders) {
        this.blockHolders = blockHolders;
        return this;
    }

    public BlockEntityTypeHolder<T> build() {
        if (blockHolders == null) blockHolders = new BlockHolder[0];
        result.type = lazyRegistry.blockEntityTypes.register(name, () -> BlockEntityType.Builder.of(((pPos, pState) -> blockEntitySupplier.create(result.get(), pPos, pState)), Arrays.stream(blockHolders).map(BlockHolder::get).toArray(Block[]::new)).build(null));
        return result;
    }
}